//
//  MeatBuyListUI.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/25.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatBuyListUIComponent.h"

@implementation MeatBuyListUIComponent

#pragma mark - private methods

- (void)drawCornerByView:(UIView *)view{
    
    CGRect rect = CGRectMake(0, 0, view.width, 120);
    UIRectCorner corners = UIRectCornerTopLeft|UIRectCornerTopRight;
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    _shopIcon.layer.mask = shape;
}


#pragma mark - setter methods

- (void)setView:(UIView *)view
{
    _view = view;
    
    [view addSubview:self.shopIcon];
    [view addSubview:self.shopNameLab];
    [view addSubview:self.shopContentLab];
    [view addSubview:self.shopSalesLab];
    [view addSubview:self.scoreLab];
    [view addSubview:self.grayView];
    [view addSubview:self.yellowView];
    
}


- (void)setType:(MeatBuyListUILayoutType)type
{
   
    if (type == MeatBuyListUILayoutType_Vertical) {
        
        [self drawCornerByView:self.view];

        {
            _shopIcon.mj_x = _shopIcon.mj_y = 0;
            _shopIcon.size = CGSizeMake(self.view.width, 120);
        }
        
        {
            _shopNameLab.mj_x = 11;
            _shopNameLab.mj_y = 13 + _shopIcon.bottom;
            _shopNameLab.width = self.view.width - 30;
        }
        
    }else{
        
        [self addShadowAndCircleCorner:_shopIcon.layer corner:5];

        {
            _shopIcon.size = CGSizeMake(100, 100);
            _shopIcon.mj_x = _shopIcon.mj_y = 15;
        }
        
        {
            _shopNameLab.mj_x = 15 + _shopIcon.right;
            _shopNameLab.mj_y = 15;
            _shopNameLab.width = KScreenWidth - _shopNameLab.left - 15;
        }
    }
    
    _shopNameLab.height = 22;

    
    {
        _shopContentLab.mj_x = _shopNameLab.left;
        _shopContentLab.mj_y = 27 +_shopNameLab.bottom;
        _shopContentLab.width = _shopNameLab.width;
        _shopContentLab.height = 34;
    }
    
    
    {
        _shopSalesLab.mj_x = _shopNameLab.left;
        _shopSalesLab.mj_y = _shopContentLab.bottom + 9;
        _shopSalesLab.width = _shopNameLab.width;
        _shopSalesLab.height = 17;
    }
    
    {
        _grayView.mj_x = _shopNameLab.left;
        _grayView.mj_y = _shopNameLab.bottom + 7;
        
        _yellowView.mj_x = _grayView.mj_x;
        _yellowView.mj_y = _grayView.mj_y;
    }
    
    {
        _scoreLab.mj_x = _grayView.right + 5;
        _scoreLab.mj_y = _grayView.top;
        _scoreLab.width = 30;
        _scoreLab.height = 17;
    }

}

- (void)setScoreNum:(CGFloat)scoreNum{
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    
    NSInteger aims = scoreNum/0.5;
    NSInteger index = aims/2;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, IconW *scoreNum + IconG*index, IconW)];
    maskLayer.path = path.CGPath;
    self.yellowView.layer.mask = maskLayer;
}


- (void)setModel:(BuyMeatModel *)model{
    
    self.shopNameLab.text = model.shopName;
    self.shopContentLab.text = model.autograph;
    NSString *saleNume = [NSString stringWithFormat:@"%@", model.saleNum];
    self.shopSalesLab.text = [@"累计销量" stringByAppendingString:saleNume];
    [self.shopIcon sd_setImageWithURL:[NSURL URLWithString:model.imgName]];
}

#pragma mark -  getter methods

- (UIImageView *)shopIcon{
    
    if (!_shopIcon) {
        _shopIcon = [[UIImageView alloc] init];
    }
    return _shopIcon;
}


//店铺名称
- (UILabel *)shopNameLab{
    
    if (!_shopNameLab) {
        _shopNameLab = [[UILabel alloc] init];
        _shopNameLab.textColor = UIColorFromRGB(0x262626);
        _shopNameLab.font = mMediumFont(16);
    }
    return _shopNameLab;
}


//店铺内容
- (UILabel *)shopContentLab{
    
    if (!_shopContentLab) {
        _shopContentLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x63656A) font:mFont(12) textAliment:0];
        _shopContentLab.numberOfLines = 0;
    }
    return _shopContentLab;
}


//店铺销量
- (UILabel *)shopSalesLab{
    
    if (!_shopSalesLab) {
        _shopSalesLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x999999) font:mFont(12) textAliment:0];
    }
    return _shopSalesLab;
}

- (UILabel *)scoreLab{
    
    if (!_scoreLab) {
        _scoreLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0xFDC60A) font:mFont(12) textAliment:0];
    }
    return _scoreLab;
}

- (UIView *)grayView{
    
    if (!_grayView) {
        _grayView = [[UIView alloc] init];
        _grayView.size = CGSizeMake(IconW *5 + IconG *4, IconW);
        
        for (NSInteger i = 0; i< 5; i++) {
            UIImageView *icon = [[UIImageView alloc] init];
            icon.frame = CGRectMake((IconW+IconG)*i , 0, IconW, IconW);
            icon.image = [UIImage imageNamed:@"meat_buy_star_gray"];
            [_grayView addSubview:icon];
        }
    }
    return _grayView;
}


- (UIView *)yellowView{
    
    if (!_yellowView) {
        _yellowView = [[UIView alloc] init];
        _yellowView.size = CGSizeMake(IconW *5 + IconG *4, IconW);
        
        for (NSInteger i = 0; i< 5; i++) {
            UIImageView *icon = [[UIImageView alloc] init];
            icon.frame = CGRectMake((IconG+IconW)*i, 0, IconW, IconW);
            icon.image = [UIImage imageNamed:@"meat_buy_star_yellow"];
            [_yellowView addSubview:icon];
        }
    }
    return _yellowView;
}

@end
